home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld 1997 September
/
Macworld (1997-09).dmg
/
Shareware World
/
Utilities
/
Text Processing
/
Alpha
/
Tcl
/
SystemCode
/
prefs.tcl
< prev
next >
Wrap
Text File
|
1997-03-28
|
7KB
|
327 lines
#===============================================================================
proc addArrDef {arr def val} {
addDef [list $arr $def] $val arr
}
proc removeArrDef {arr def} {
removeDef [list $arr $def] arr
}
proc addDef {def val {prefix {}}} {
global ${prefix}prefDefs
readDefs $prefix
set ${prefix}prefDefs($def) $val
writeDefs $prefix
catch {unset ${prefix}prefDefs}
}
proc removeDef {def {prefix {}}} {
global ${prefix}prefDefs
readDefs $prefix
catch {unset ${prefix}prefDefs($def)}
writeDefs $prefix
catch {unset ${prefix}prefDefs}
}
proc readDefs {{prefix {}}} {
global HOME PREFS globalPrefix
if {![file exists "$PREFS:${prefix}defs.tcl"]} return
set globalPrefix $prefix
uplevel #0 {source "$PREFS:${globalPrefix}defs.tcl"}
}
proc writeDefs {{prefix {}}} {
global HOME PREFS ${prefix}prefDefs
if {![info exists ${prefix}prefDefs]} {
catch {removeFile "$PREFS:${prefix}defs.tcl"}
return
}
if {![file exists "$PREFS"]} {
mkdir "$PREFS"
}
set fd [open "$PREFS:${prefix}defs.tcl" "w"]
foreach nm [array names ${prefix}prefDefs] {
puts $fd [list set ${prefix}prefDefs($nm) [set ${prefix}prefDefs($nm)]]
}
close $fd
}
proc readUserDefs {} {
global prefDefs arrprefDefs PREFS
if {[file exists "$PREFS:defs.tcl"]} {
source "$PREFS:defs.tcl"
foreach nm [array names prefDefs] {
global $nm
set $nm $prefDefs($nm)
}
catch {unset prefDefs}
}
if {[file exists "$PREFS:arrdefs.tcl"]} {
source "$PREFS:arrdefs.tcl"
foreach nm [array names arrprefDefs] {
set arr [lindex $nm 0]
set field [lindex $nm 1]
set val $arrprefDefs($nm)
global $arr
set ${arr}($field) $val
}
catch {unset arrprefDefs}
}
# Use "prefs.tcl" to define or change any tcl information. */
if {![file exists "$PREFS:prefs.tcl"]} {
if {![file exists "$PREFS"]} {
mkdir "$PREFS"
}
set fd [open "$PREFS:prefs.tcl" "w"]
close $fd
}
uplevel #0 {
if {[catch {source "$PREFS:prefs.tcl"}]} {
alertnote "An error occurred while loading \"prefs.tcl\""
}
}
}
proc viewSavedSetting {} {
global prefDefs arrprefDefs
saveModifiedVars
readDefs
readDefs arr
set res [listpick -p "The following settings have been saved:" [getSavedSettings]]
if {[regexp {([^(]+)\(([^)]+)\)} $res dummy arr field]} {
set arg [list $arr $field]
set val $arrprefDefs($arg)
} else {
global $res
set val $prefDefs($res)
}
viewValue $res $val
unset prefDefs arrprefDefs
}
proc viewValue {name val} {
if {[string length $val] > 80} {
if {([llength $val] > 3) && ([llength $val] > 6 || [string length $val] > 160)} {
listpick -p "'$name's value is:" $val
} else {
global tileLeft tileTop tileWidth
new -g $tileLeft $tileTop $tileWidth 100 -n "\$$name"
insertText "'$name's value is:\r\r$val\r"
setWinInfo dirty 0
setWinInfo read-only 1
goto 0
}
} else {
alertnote "'$name's value is:\r\r$val\r"
}
}
proc removeSavedSetting {} {
global prefDefs arrprefDefs
saveModifiedVars
readDefs
readDefs arr
set res [listpick -p "Remove which setting?" [lsort -ignore [getSavedSettings]]]
if {$res == ""} return
if {[regexp {([^(]+)\(([^)]+)\)} $res dummy arr field]} {
global $arr
# catch {unset ${arr}($field)}
removeArrDef $arr $field
} else {
global $res
# catch {unset $res}
removeDef $res
}
unset prefDefs arrprefDefs
}
proc getSavedSettings {} {
global prefDefs arrprefDefs
readDefs
readDefs arr
set names [array names prefDefs]
foreach pair [array names arrprefDefs] {
lappend names "[lindex $pair 0]([lindex $pair 1])"
}
return [lsort $names]
}
#===============================================================================
proc pushRecent {name} {
global recentFiles
regsub { <\w+>} $name {} name
if {[string match {*\**} $name] || [string match "Untit*" $name] } return
if {[info exists recentFiles] && ([set ind [lsearch -glob $recentFiles "*[file tail $name]"]] >= 0)} {
set recentFiles [lreplace $recentFiles $ind $ind]
}
lappend recentFiles $name
if {[llength $recentFiles] > 10} {
set recentFiles [lrange $recentFiles 1 end]
}
set tails [map {file tail} $recentFiles]
menu -m -c -n recent -p recentProc [concat [lsort -ignore $tails] [list "(-" "Reset List"]]
}
proc recentProc {menu name} {
global recentFiles
if {$name == "Reset List"} {
set recentFiles {}
menu -m -n recent -p recentProc {}
} else {
if {[set ind [lsearch $recentFiles "*:$name"]] >= 0} {
edit [lindex $recentFiles $ind]
}
}
}
proc buildRecentFiles {} {
global recentFilesSave recentFiles modifiedVars
lappend modifiedVars
message "Building recent files menu…"
if {[info exists recentFiles]} {
set recentFilesSave $recentFiles
}
if {[info exists recentFilesSave]} {
foreach f $recentFilesSave {
pushRecent $f
}
}
catch {unset recentFilesSave}
}
proc appendText {t item} {return "$item$t"}
proc editRecentFile {} {
global recentFiles
foreach f $recentFiles {
lappend tails [file tail $f]
}
foreach res [listpick -l -p {File?} [lsort -ignore $tails]] {
set ind [lsearch $recentFiles \*:$res]
catch {edit [lindex $recentFiles $ind]}
}
}
#===============================================================================
proc editPrefs {} {
global PREFS
if {![file exists "$PREFS:prefs.tcl"]} {
set fd [open "$PREFS:prefs.tcl" "w"]
close $fd
}
edit "$PREFS:prefs.tcl"
}
# Automatically add a line to the user input file
proc addUserLine {line} {
global HOME PREFS
if {![file exists "$PREFS"]} {
mkdir "$PREFS"
}
set fid [open "$PREFS:prefs.tcl" "a+"]
if {![catch {seek $fid -1 end}]} {
if {[read $fid 1] == "\r"} {
set line "\r$line"
}
}
seek $fid 0 end
puts $fid $line
close $fid
}
#===============================================================================
proc sourceCurrentModePrefs {} {
global mode PREFS
if {[file exists "${PREFS}:${mode}Prefs.tcl"]} {
source "${PREFS}:${mode}Prefs.tcl"
} else {
beep; message "Sorry, no preferences for '$mode' mode"
}
}
proc editCurrentModePrefs {} {
global mode
editPrefsProc modePrefs $mode
}
proc editPrefsProc { menu m } {
message $m
global PREFS mode
# assume it is a mode, since we made the menu
set f "$PREFS:${m}Prefs.tcl"
if [file exists $f] {
edit $f
} else {
if { [askyesno "No '$m' prefs file exists, do you want to create one?"] \
== "yes" } {
set fd [open $f "w"]
close $fd
edit $f
insertText {##
# This file will be sourced automatically, immediately after
# the _first_ time the file which defines its mode is sourced.
# Use this file to insert your own mode-specific preferences
# and changes, rather than altering the originals.
#
# You can redefine menus, procedures, variables,...
##
}}}
}